package core;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

public class Core {
	static private Core instance = null;
	private XmlEngine xml = null;
	private Calculator calculator = new Calculator();

	private Core() {
		xml = XmlEngine.get();
		xml.load();
	}

	public static Core get() {
		if (instance == null)
			instance = new Core();
		return instance;
	}

	public void save() {
		xml.save();
	}

	public void exportXml(String path) {
		xml.save(path);
	}

	public void importXml(String path) {
		xml.load(path);
		xml.save();
	}

	public List<UserEntry> getEntries() {
		return xml.getEntries();
	}

	public List<UserEntry> getEntriesUntil(Date until) {
		List<UserEntry> all = xml.getEntries();
		ArrayList<UserEntry> newlist = new ArrayList<UserEntry>();
		Iterator<UserEntry> i = all.iterator();
		while (i.hasNext()) {
			UserEntry cur = i.next();
			if (cur.getDate().after(until))
				break;
			newlist.add(cur);
		}
		return newlist;
	}

	public List<UserEntry> getEntriesBetween(Date begin, Date end) {
		List<UserEntry> all = xml.getEntries();
		ArrayList<UserEntry> newlist = new ArrayList<UserEntry>();
		Iterator<UserEntry> i = all.iterator();
		while (i.hasNext()) {
			UserEntry cur = i.next();
			if (cur.getDate().after(end))
				break;
			if (!cur.getDate().before(begin))
				newlist.add(cur);
		}
		return newlist;
	}

	public List<UserEntry> getEntriesOf(Category cat) {
		return getEntriesOf(cat.getId());
	}

	public List<UserEntry> getEntriesOf(int categoryId) {
		List<UserEntry> all = xml.getEntries();
		ArrayList<UserEntry> newlist = new ArrayList<UserEntry>();

		Iterator<UserEntry> i = all.iterator();
		while (i.hasNext()) {
			UserEntry entry = (UserEntry) i.next();
			if (entry.getCategoryId() == categoryId)
				newlist.add(entry);
		}
		return newlist;
	}

	public List<UserEntry> getEntriesOfBetween(int categoryId, Date begin,
			Date end) {
		List<UserEntry> all = getEntriesBetween(begin, end);
		ArrayList<UserEntry> newlist = new ArrayList<UserEntry>();
		Iterator<UserEntry> i = all.iterator();
		while (i.hasNext()) {
			UserEntry entry = i.next();
			if (entry.getCategoryId() == categoryId)
				newlist.add(entry);
		}
		return newlist;
	}

	public List<UserEntry> getEntriesOfUntil(int categoryId, Date until) {
		List<UserEntry> all = getEntriesUntil(until);
		ArrayList<UserEntry> newlist = new ArrayList<UserEntry>();
		Iterator<UserEntry> i = all.iterator();
		while (i.hasNext()) {
			UserEntry cur = i.next();
			if (cur.getCategoryId() == categoryId)
				newlist.add(cur);
		}
		return newlist;
	}

	public List<UserEntry> getEntriesFor(int month, int year) {
		Date a = Calculator.beginOf(year, month);
		Date b = Calculator.endOf(year, month);
		return getEntriesBetween(a, b);
	}

	public List<UserEntry> getEntriesSearch(String word) {
		ArrayList<UserEntry> newlist = new ArrayList<UserEntry>();

		// Chercher l'id de la categorie qui porte son nom
		int catId = -1;
		List<Category> categories = getCategories();
		Iterator<Category> j = categories.iterator();
		while (j.hasNext()) {
			Category cur = j.next();
			if (cur.getName().equals(word)) {
				catId = cur.getId();
				break;
			}
		}

		List<UserEntry> all = getEntries();
		Iterator<UserEntry> i = all.iterator();
		while (i.hasNext()) {
			UserEntry cur = i.next();
			if (cur.getCategoryId() == catId)
				newlist.add(cur);
			else{
				if (cur.getComment().indexOf(word) != -1)
					newlist.add(cur);
			}
		}

		return newlist;
	}

	public UserEntry addEntry(float value, String comment, int catId, Date date) {
		return xml.addEntry(value, comment, catId, date);
	}

	public void deleteEntry(UserEntry delEntry) {
		xml.deleteEntry(delEntry);
	}

	public List<Category> getCategories() {
		return xml.getCategories();
	}

	public String getCategoryName(int id) {
		return xml.getCategoryName(id);
	}

	public Category addCategory(String name) {
		return xml.addCategory(name);
	}

	public void deleteCategory(int id) {
		xml.deleteCategory(id);
	}

	public Calculator getCalculator() {
		return calculator;
	}

	public void setNote(String note) {
		xml.setNote(note);
	}

	public String getNote() {
		return xml.getNote();
	}
}
